<?php
/*
 * invoke example:
 * symfony wac:initData truncate
 */
class wacInitDataTask extends sfBaseTask
{
  protected function configure()
  {
    // // add your own arguments here
    // $this->addArguments(array(
    //   new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),
    // ));
    $this->addArguments(array(
       new sfCommandArgument('action', sfCommandArgument::REQUIRED, 'Do acton'),
     ));

    $this->addOptions(array(
      new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
      new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'wac_storehouse'),
      // add your own options here
    ));

    $this->namespace        = 'wac';
    $this->name             = 'initData';
    $this->briefDescription = '';
    $this->detailedDescription = <<<EOF
The [wac:initData|INFO] task does things.
Call it with: action

  [php symfony wac:initData|INFO]
EOF;
  }

  protected function execute($arguments = array(), $options = array())
  {
    // initialize the database connection
    $databaseManager = new sfDatabaseManager($this->configuration);
    $connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection();



    if(isset($arguments['action']))
    {
        switch($arguments['action'])
        {
            case 'test':
                $this->runTest();
                break;
            case 'truncate':
                $this->runTruncateTables();
                break;
            default:
                $this->log("no action");
                $this->log("arguments: ");
                $this->log(print_r($arguments,true));
                break;
        }

    }

//    $this->log("arguments: ");
//    $this->log(print_r($arguments,true));
//
//    $this->log("options: ");
//    $this->log(print_r($options,true));

    $this->log("script finished.");
  }

  /*
   * return truncate tables list
   */
  protected function getTruncateTableList()
  {
      $list = array();
//      $list[] = WacTable::$axis;
      $list[] = WacTable::$businessLog;
      $list[] = WacTable::$cleanUpForm;
      $list[] = WacTable::$cleanUpFormItem;
      $list[] = WacTable::$cleanUpQcForm;
      $list[] = WacTable::$cleanUpQcFormItem;
//      $list[] = WacTable::$color;
      $list[] = WacTable::$cottonOrder;
      $list[] = WacTable::$cottonOrderGoodsItem;
//      $list[] = WacTable::$country;
//      $list[] = WacTable::$currency;
//      $list[] = WacTable::$customer;
      $list[] = WacTable::$customerParameter;
      $list[] = WacTable::$defectiveProductForm;
      $list[] = WacTable::$defectiveProductFormItem;
      $list[] = WacTable::$dyeOrder;
      $list[] = WacTable::$dyeOrderItem;
      $list[] = WacTable::$dyeOrderItemCottonConsume;
//      $list[] = WacTable::$factory;
      $list[] = WacTable::$factoryGoodsItem;
//      $list[] = WacTable::$factoryParameter;
//      $list[] = WacTable::$factoryType;
//      $list[] = WacTable::$fillingYarnArrangement;
      $list[] = WacTable::$finalClothForm;
      $list[] = WacTable::$finalClothFormItem;
//      $list[] = WacTable::$formula;
//      $list[] = WacTable::$freightSpace;
//      $list[] = WacTable::$goodsCategory;
      $list[] = WacTable::$goodsCategoryUnit;
//      $list[] = WacTable::$jar;
//      $list[] = WacTable::$language;
      $list[] = WacTable::$customerOrder;
      $list[] = WacTable::$productionOrder;
      $list[] = WacTable::$productionOrderCleanUpQcForm;
      $list[] = WacTable::$productionOrderGoodsItem;
      $list[] = WacTable::$productionOrderWeaveQcForm;
//      $list[] = WacTable::$rawMaterial;
//      $list[] = WacTable::$spinner;
//      $list[] = WacTable::$standard;
//      $list[] = WacTable::$storehouse;
      $list[] = WacTable::$storehouseGoodsItem;
//      $list[] = WacTable::$storehouseParameter;
//      $list[] = WacTable::$supplier;
//      $list[] = WacTable::$supplierParameter;
//      $list[] = WacTable::$sysmsg;
      $list[] = WacTable::$systemLog;
//      $list[] = WacTable::$systemParameter;
      $list[] = WacTable::$template;
//      $list[] = WacTable::$unit;
//      $list[] = WacTable::$warpArrangement;
      $list[] = WacTable::$weaveOrder;
      $list[] = WacTable::$weaveOrderItem;
      $list[] = WacTable::$weaveOrderItemCottonConsume;
      $list[] = WacTable::$weaveOrderItemWarpYarnConsume;
      $list[] = WacTable::$weaveQcForm;
      $list[] = WacTable::$weaveQcFormItem;
//      $list[] = WacTable::$workflow;
//      $list[] = WacTable::$workflowItem;
      $list[] = WacTable::$productionOrderEstimateGoodsItem;
//      $list[] = WacTable::$sampleJar;
//      $list[] = WacTable::$wrapper;
//      $list[] = WacTable::$wacGuardUser;
//      $list[] = WacTable::$wacGuardGroup;
//      $list[] = WacTable::$wacGuardPermission;
      $list[] = WacTable::$dispatchOrder;
      $list[] = WacTable::$dispatchOrderItem;
      $list[] = WacTable::$stockGoodsStatement;

      $list[] = WacTable::$cleanUpFormItemConsume;
      $list[] = WacTable::$finalClothFormItemConsume;

      $list[] = WacTable::$finalClothQcWrapperForm;
      $list[] = WacTable::$finalClothQcTestForm;
      $list[] = WacTable::$finalClothQcTestFormItem;
      $list[] = WacTable::$finalClothQcTestHistory;
      return $list;
  }

  /*
   * run truncate command
   */
  protected function runTruncateTables()
  {
      $this->log("turn off foreign key checkes:");
      WacCommonTable::turnOffForeignKey();

      $this->log("truncate table:");
      $list = $this->getTruncateTableList();
      if(count($list)>0)
      {
          foreach($list as $tableName)
          {
              Doctrine::getTable($tableName)->truncate();
          }
      }
  }

  protected function runTest()
  {
      $this->log("test:");
      $objQuery = Doctrine::getTable(WacTable::$dyeOrder)->createQuery('v')->select('v.*');
      $dyeOrders = $objQuery->execute();
      if(count($dyeOrders)>0) {
          $i=0;
          foreach($dyeOrders as $dyeOrder) {
//            echo "{$i}: ".$dyeOrder->getId().", \n";
              $this->log("{$i}: ".$dyeOrder->getId());
              $i++;
          }
      }
  }
}
